/**
 * 
 */
package com.telenav.techtalk;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

/**
 * @author yxyao
 *
 */
public class GifActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Movie mv = Movie.decodeStream(getResources().openRawResource(R.drawable.progress));
        setContentView(new GifView(this, mv), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }
    
    class GifView extends View{

        private long firstUpdateTime;
        private Movie mv;
        /**
         * @param context
         */
        public GifView(Context context, Movie mv) {
            super(context);
            this.mv = mv;
        }
        
        protected void onDraw(Canvas canvas) {
            long now = SystemClock.uptimeMillis();
            int runingTime = 0;
            if(0 == firstUpdateTime)
            {
                firstUpdateTime = now;
            }
            else{
                runingTime =(int)(now - firstUpdateTime);
            }
            int duration = mv.duration();
            if(duration>0){
                mv.setTime(runingTime%duration);
                invalidate();
            }
            mv.draw(canvas, 0, 0);
        }
        
    }
}
